#include "htmlform.h"
#include "ui_htmlform.h"

htmlForm::htmlForm(QWidget *parent) :
    QWidget(parent),
    m_ui(new Ui::htmlForm)
{
    m_ui->setupUi(this);

    m_ui->html83->setChecked(false);
    m_ui->htmlCss->setChecked(false);
    m_ui->htmlColors->setChecked(false);
    m_ui->htmlFileLinks->setChecked(false);
    m_ui->htmlFolderLinks->setChecked(false);
    m_ui->htmlComboBox->setCurrentIndex(0);

    m_ui->headerColor->setStyleSheet( "background-color: rgb(0,0,0);" );
    m_ui->header2Color->setStyleSheet( "background-color: rgb(0,0,0);" );
    m_ui->textColor->setStyleSheet( "background-color: rgb(0,0,0);" );
    m_ui->linkColor->setStyleSheet( "background-color: rgb(0,0,255);" );
}

htmlForm::~htmlForm()
{
    delete m_ui;
}

void htmlForm::changeEvent(QEvent *e)
{
    QWidget::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        m_ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

bool htmlForm::html83format()
{ return m_ui->html83->isChecked(); }

bool htmlForm::htmlCss()
{ return m_ui->htmlCss->isChecked(); }

bool htmlForm::htmlColors()
{ return m_ui->htmlColors->isChecked(); }

bool htmlForm::htmlFilesLinks()
{ return m_ui->htmlFileLinks->isChecked(); }

bool htmlForm::htmlFoldersLinks()
{ return m_ui->htmlFolderLinks->isChecked(); }

int htmlForm::getHtmlOutputFormat()
{ return m_ui->htmlComboBox->currentIndex(); }

QString htmlForm::getCssFile()
{ return m_ui->cssFileLine->text(); }

QString htmlForm::getHeaderColor()
{ return getHexColorValue( m_ui->headerColor ); }

QString htmlForm::getHeader2Color()
{ return getHexColorValue( m_ui->header2Color ); }

QString htmlForm::getTextColor()
{ return getHexColorValue( m_ui->textColor ); }

QString htmlForm::getLinkColor()
{ return getHexColorValue( m_ui->linkColor ); }

void htmlForm::on_toolButton_clicked()
{
    QString fileName = QFileDialog::getOpenFileName(this, tr("Open File"),
                                                    "", tr("Files (*.css)"));
    if ( !fileName.isEmpty() )
    {
        if ( QFile::exists( fileName ) )
            m_ui->cssFileLine->setText( fileName );
        else
        {
            QMessageBox::critical( this, "Error",
                                   "Cannot find the file in given path!" );
            m_ui->cssFileLine->selectAll();
            m_ui->toolButton->setFocus();
        }
    }
}

void htmlForm::on_headerButton_clicked()
{
    QColor h1Color = QColorDialog::getColor ( QColor(0, 0, 0), this, "header color picking" );

    int r = h1Color.red();
    int g = h1Color.green();
    int b = h1Color.blue();

    QString inStr = QString::number(r) + ", " + QString::number(g) + ", " + QString::number(b);
    m_ui->headerColor->setStyleSheet( "background-color: rgb(" + inStr + ");" );
}

void htmlForm::on_header2Button_clicked()
{
    QColor h1Color = QColorDialog::getColor ( QColor(0, 0, 0), this, "header color picking" );

    int r = h1Color.red();
    int g = h1Color.green();
    int b = h1Color.blue();

    QString inStr = QString::number(r) + ", " + QString::number(g) + ", " + QString::number(b);
    m_ui->header2Color->setStyleSheet( "background-color: rgb(" + inStr + ");" );
}

void htmlForm::on_textButton_clicked()
{
    QColor h1Color = QColorDialog::getColor ( QColor(0, 0, 0), this, "header color picking" );

    int r = h1Color.red();
    int g = h1Color.green();
    int b = h1Color.blue();

    QString inStr = QString::number(r) + ", " + QString::number(g) + ", " + QString::number(b);
    m_ui->textColor->setStyleSheet( "background-color: rgb(" + inStr + ");" );
}

void htmlForm::on_linkButton_clicked()
{
    QColor h1Color = QColorDialog::getColor ( QColor(0, 0, 255), this, "header color picking" );

    int r = h1Color.red();
    int g = h1Color.green();
    int b = h1Color.blue();

    QString inStr = QString::number(r) + ", " + QString::number(g) + ", " + QString::number(b);
    m_ui->linkColor->setStyleSheet( "background-color: rgb(" + inStr + ");" );
}

QString htmlForm::getHexColorValue( QLabel * lab )
{
    QString retStr = "";
    QString styleSheet1 = lab->styleSheet();
    styleSheet1.remove( "background-color: rgb(" );
    styleSheet1.remove( ");" );

    QStringList colors = styleSheet1.split( ',', QString::SkipEmptyParts );
    if ( colors.count() > 0 )
    {
        for (int i = 0; i < colors.count(); i++ )
        {
            QString cl = QString (colors.at(i)).simplified();
            bool ok;
            int dec = cl.toInt(&ok);
            if ( ok )
                cl = QString("%1").arg(dec, 0, 16);
            if ( cl.length() == 1 )
                cl.prepend( "0" );
            retStr.append( cl );
        }
    }
    return retStr;
}

QString htmlForm::getStyleSheetValue( QString hexRep )
{
    QString retStr = "background-color: rgb(";

    bool ok;
    QString red   =  hexRep.left (2);
    red = QString::number( red.toInt( &ok, 16 ) );
    QString green =  hexRep.mid(3,2);
    green = QString::number( green.toInt( &ok, 16 ) );
    QString blue  =  hexRep.right(2);
    blue = QString::number( blue.toInt( &ok, 16 ) );

    retStr += red + ", " + green + ", " + blue;
    retStr += ");";
    return retStr;
}
